//
//  OMConsumerGoodsCollectDelegater.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/4/22.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMConsumerGoodsCollectDelegater.h"
#import "MainConfigure.h"
#import <UIKit/UIKit.h>

#import "OMGoodCollectCell.h"
#import "OMGoodOneColumnCollectCell.h"
#import "OMSalesRankCollectCell.h"

#import "OMConsumerGoodTwoColumnLayout.h"
#import "OMConsumerGoodOneColumnLayout.h"
#import "OMGoodOneColumnLayout.h"

#import "UIScrollView+EmptyDataSet.h"


@interface OMConsumerGoodsCollectDelegater()<UICollectionViewDelegate,UICollectionViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic, weak) UICollectionView *collectView;
@end
@implementation OMConsumerGoodsCollectDelegater
- (instancetype)initWithCollectionView:(UICollectionView *)collectView
{
    self = [super init];
    _collectView = collectView;
    [self configure];
    return self;
}

- (UIImage *)emptyImage
{
    return _emptyImage?:[UIImage st_imageNamed:@"ic_goods_empty@2x" type:@"png"];
}

- (NSString *)emptyDataMessage
{
    return _emptyDataMessage?:@"唉呀,没找到相应商品!";;
}

- (void)configure
{
    [self.collectView registerClass:[OMGoodOneColumnCollectCell class] forCellWithReuseIdentifier:[OMGoodOneColumnCollectCell cellIdentifier]];
    [self.collectView registerClass:[OMGoodCollectCell class] forCellWithReuseIdentifier:[OMGoodCollectCell cellIdentifier]];
    [self.collectView registerClass:[OMSalesRankCollectCell class] forCellWithReuseIdentifier:[OMSalesRankCollectCell cellIdentifier]];
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    self.collectView.emptyDataSetSource = self;
    self.collectView.emptyDataSetDelegate = self;
    self.collectView.backgroundColor = SM_backGroundColor;
    self.collectView.alwaysBounceVertical = true;
}

- (void)changeLayout
{
    if ([self.collectView.collectionViewLayout isKindOfClass:[OMConsumerGoodTwoColumnLayout class]]) {
        [self.collectView setCollectionViewLayout:[OMConsumerGoodOneColumnLayout new] animated:false];
        self.collectView.backgroundColor = SM_whiteColor;
    }else {
        BOOL needScroll = self.collectView.contentOffset.y == 0;
        [self.collectView setCollectionViewLayout:[OMConsumerGoodTwoColumnLayout new] animated:false];
        self.collectView.backgroundColor = SM_backGroundColor;
        !needScroll?:[self.collectView setContentOffset:CGPointMake(0, 0)];
    }
    [self.collectView reloadData];
}


- (void)setGoods:(NSArray *)goods
{
    _goods = goods;
    [self.collectView reloadData];
    [self.collectView reloadEmptyDataSet];
}

#pragma mark - delegate && datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.goods.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.collectView.collectionViewLayout isKindOfClass:[OMConsumerGoodTwoColumnLayout class]]) {
        OMGoodCollectCell *cell = [OMGoodCollectCell collectCellWithCollectionView:collectionView atIndexPath:indexPath];
        [cell configureCellWithObj:self.goods[indexPath.item]];
        cell.layer.borderColor = HEXCOLOR(@"#E9E9EA").CGColor;
        cell.layer.cornerRadius = SM_SIZE(5);
        cell.clipsToBounds = true;
        cell.layer.borderWidth = 1.f;
        cell.contentView.backgroundColor = SM_whiteColor;
        return cell;
    }else if ([self.collectView.collectionViewLayout isKindOfClass:[OMGoodOneColumnLayout class]]) {
        OMSalesRankCollectCell *cell = [OMSalesRankCollectCell collectCellWithCollectionView:collectionView atIndexPath:indexPath];
        [cell configureCellWithObj:self.goods[indexPath.item]];
        cell.contentView.backgroundColor = SM_whiteColor;
        return cell;
    }

    OMGoodOneColumnCollectCell *cell = [OMGoodOneColumnCollectCell collectCellWithCollectionView:collectionView atIndexPath:indexPath];
    [cell configureCellWithObj:self.goods[indexPath.item]];

    cell.contentView.backgroundColor = SM_whiteColor;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NaviPush(@"OMGoodDetailViewController", @{@"productId":[self.goods[indexPath.item] id]});
}

#pragma mark - DZNEmptyDataSetSource
//空tableview 的处理(完全得益于DZNEmptyDataSetSource),获得该处理，子类需要遵守相关的协议并设置相关的代理
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = nil;
    UIFont *font = nil;
    UIColor *textColor = nil;
    NSMutableDictionary *attributes = [NSMutableDictionary new];

    text = self.emptyDataMessage;
    font = SM_CFont(32);
    textColor = SM_blackColor;
    [attributes setObject:@(0.45) forKey:NSKernAttributeName];
    if (!text) {
        return nil;
    }
    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    if (self.isLoading) {
        return nil;
    }
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    NSString *text = nil;
    UIFont *font = nil;
    UIColor *textColor = nil;
    NSMutableDictionary *attributes = [NSMutableDictionary new];

    text = @"去搜索";
    font = SM_CBFont(31);
    textColor = SM_thumeColor;
    [attributes setObject:@(1) forKey:NSKernAttributeName];
    if (!text) {
        return nil;
    }
    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    if (self.isLoading) {
        return nil;
    }
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    if (self.isLoading) {
        return nil;
    }
    return self.emptyImage;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    //to override
    NaviPush(@"OMGoodSearchViewController", nil);
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
{
    return SM_SIZE(100);
}

// empty data function system core
- (void)setLoading:(BOOL)loading
{
    _loading = loading;
    [self.collectView reloadEmptyDataSet];
}
@end
